package com.zhsq.zlc.controller.log;

import com.zhsq.zlc.entity.IDEntity;
import com.zhsq.zlc.entity.LogEntity;
import com.zhsq.zlc.entity.ResultObject;
import com.zhsq.zlc.service.log.LogService;
import com.zhsq.zlc.util.Constant;
import com.zhsq.zlc.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class LogController {

	@Autowired
	private LogService logService;

	/**
	 * 日志列表
	 * @param entity
	 * @param pageCurrent
	 * @param pageSize
	 * @param pageCount
	 * @param model
	 * @return
	 */
	@RequestMapping("/zhsq/logManage_{pageCurrent}_{pageSize}_{pageCount}")
	public String logManage(LogEntity entity, @PathVariable Integer pageCurrent, @PathVariable Integer pageSize, @PathVariable Integer pageCount, Model model) throws Exception{
		//判断
		if(pageSize == 0) pageSize = 10;
		if(pageCurrent == 0) pageCurrent = 1;
		int rows = logService.count(entity);
		if(pageCount == 0) pageCount = rows%pageSize == 0 ? (rows/pageSize) : (rows/pageSize) + 1;

		//查询
		entity.setStart((pageCurrent - 1)*pageSize);
		entity.setEnd(pageSize);
		List<LogEntity> list = logService.list(entity);
		if(list!=null && list.size()>0){
			for (int i=0;i<list.size();i++){
				list.get(i).setDataIndex(entity.getStart()+i+1);
			}
		}
		//输出
		model.addAttribute("list", list);
		String pageHTML = PageUtil.getPageContent("logManage_{pageCurrent}_{pageSize}_{pageCount}?content="+entity.getContent(), pageCurrent, pageSize, pageCount);
		System.err.println("logManage  pageHTML="+pageHTML);
		model.addAttribute("pageHTML",pageHTML);
		model.addAttribute("log",entity);
		return "zhsq/logManage";
	}


	/**
	 * 删除日志
	 * @param entity
	 * @return
	 */
	@ResponseBody
	@PostMapping("/zhsq/logDelete")
	public ResultObject<Object> logDelete(IDEntity entity) throws Exception{
		logService.delete(entity.getIntID());
		ResultObject<Object> object = new ResultObject<Object>(Constant.Code01, Constant.Msg01,null, null);
		return object;
	}
}
